package com.zhongshengnetwork.rightbe.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
//import android.support.v7.app.AppCompatActivity;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;

public class WXEntryActivity extends AppBaseActivity implements IWXAPIEventHandler {
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomApplication.mWxApi.handleIntent(getIntent(),WXEntryActivity.this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        CustomApplication.mWxApi.handleIntent(intent, WXEntryActivity.this);
    }

    @Override
    public void onReq(BaseReq req) {
        System.out.print("微信请求类型："+req.getType()+"\n");
    }

    @Override
    public void onResp(BaseResp resp) {
        System.out.print("微信类型："+resp.getType()+"\n");
        if (ConstantsAPI.COMMAND_PAY_BY_WX == resp.getType()){//微信支付
            if (resp.errCode==0){
                Intent intent = new Intent();
                intent.setAction(BroadcastDefine.WX_Pay_Result);
                intent.putExtra("errCode", resp.errCode);
                sendBroadcast(intent);
                finish();
            }else {
                Intent intent = new Intent();
                intent.setAction(BroadcastDefine.WX_Pay_Result);
                intent.putExtra("errCode", resp.errCode);
                sendBroadcast(intent);
                finish();
            }
        }else if(ConstantsAPI.COMMAND_SENDAUTH == resp.getType()){//微信授权登陆
            Intent intent = new Intent();
            intent.setAction(BroadcastDefine.WX_Login);
            intent.putExtra("errCode", resp.errCode);
            if (resp.errCode == 0){
                String code = ((SendAuth.Resp) resp).code;
                intent.putExtra(APIKey.codeKey,code);
            }
            sendBroadcast(intent);
            finish();
        }else {
            if (resp.errCode==0){
                //ToastUtil.show(WXEntryActivity.this,"分享成功");
                Intent intent = new Intent();
                intent.setAction(BroadcastDefine.WX_Share_Result);
                sendBroadcast(intent);
            }
            finish();
        }

    }

}
